<?php

namespace App\Http\Controllers\Ctype;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use App\Http\Controllers\Controller;
use App\Utils\FileUploader;
use App\Models\Ctype;
use App\Models\CourseClass;
class CtypeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

		$cType = new Ctype();
		$data = $cType->gType();
		$tree = $cType->getTree($data,0);
		//dd($tree);

		return view("ctype.index",['data'=>$tree]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {	

		$cType = new Ctype();
		$data = $cType->gType();
		$tree = $cType->getTree($data,0);

		return view("ctype.add",['type'=>$tree]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
		
		$up = new FileUploader();
		//±£´æ
		$data=$request->input();
		$arr =  $up->upload($request,'cover_url');

		array_pop($data);

		unset($data['_token']);
		$data['cover_url'] = $arr['url'];
		$data['created_at'] = date("Y-m-d H:i:s");
		$re = DB::table("course_type")->insert($data);
		if($re){
		  return	redirect("ctype/list");
		}else{
		  return false;
		}
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        //
        $id = $request->input("id");
        $cType = new Ctype();
        $class = $cType->getone($id);
        //dd($class);
        return view("ctype.show",['title'=>$class[0]]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        //
		//dd($data);
		$cType = new Ctype();
		$type = $cType->gType();
		$tree = $cType->getTree($type,0);
		$id = $request->input("id");
		$data = DB::table("course_type")->where("id",$id)->get();

		return view("ctype.cedit",["data"=>$data[0],"tree" =>$tree]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {

		$data=$request->input();
		$id = $data["id"];
		array_pop($data);

		if($request->hasFile('cover_url')){
			$up = new FileUploader();
			$arr =  $up->upload($request,'cover_url');

			$data['cover_url'] = $arr['url'];
		}
		unset($data['_token']);
		$data['updated_at'] = date("Y-m-d H:i:s");
		$re = Ctype::where('id',$id)->update($data);

		if($re){
			$this->getSessionInfo($request,'success','编辑成功');
			return	redirect("ctype/list");
		}else{
			$this->getSessionInfo($request,'error','编辑失败');
			return redirect("ctype/list");
		}
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        //
		$id=$request->input('id');
        $arr=DB::table('course_type')->where('id',$id)->delete();
        if($arr){
			return	redirect("ctype/list");
		}
    }

	/**
	 * 栏目
	 */
	/**
	 * 获取栏目
	 */

	public function getCate(Request $request){
		$id = $request->input("id");
		$data = DB::table("course_type")->where("pid",$id)->select("id","title")->get()->toArray();
		return json_encode($data);
	}


    /**
     * 查看具体课程
     */
    public function cindex($level,$id){
        if($level == 1){
            $type = Ctype::where("pid",$id)->whereNull("deleted_at")->where("state",1)->select("id")->get()->toArray(); 
            if(count($type) > 0){
                foreach($type as $k=>$v){
                    $arr[$k] = $v['id'];
                }
                $con = Ctype::whereIn("pid",$arr)->whereNull("deleted_at")->where("state",1)->select("id")->get()->toArray(); 
                foreach($con as $k=>$v){
                    $arr2[$k] = $v['id'];
                }
            }else{
                $arr2 = [];
            }

        }elseif($level == 2){
            $con = Ctype::where("pid",$id)->whereNull("deleted_at")->where("state",1)->select("id")->get()->toArray(); 
            foreach($con as $k=>$v){
                $arr2[$k] = $v['id'];
            }
        }elseif($level == 3){
            $arr2[] = $id;
        }
        $data = CourseClass::whereIn("course_type_id",$arr2)->whereNull("deleted_at")->where("state",1)->get()->toArray();
        $title = Ctype::where("id",$id)->select("title")->first();
       // dd($title);
        $this->ret["data"] = $data;
        $this->ret["title"] = $title;
        return view("ctype/cindex",$this->ret);
    }
}
